﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace XMLoppgave
{
    public partial class Form1 : Form
    {
        List<string> _items = new List<string>();
        
        public Form1()
        {
            InitializeComponent();
        }



        private void tabPage1_Click(object sender, EventArgs e)
        {
        }

        private void button3_Click(object sender, EventArgs e)
        {
            MessageBox.Show(textBox3.Text);
        }
        
        private void listboksen()
        {
            //_items.Add("Test");
            _items.Add(textBox7.Text);
            _items.Add(textBox2.Text);
            _items.Add(textBox5.Text);

            listBox1.DataSource = _items; 
            
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox4.Text = string.Empty;
            textBox1.Text = string.Empty;
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "XML|*.xml";
            openFileDialog1.Title = "Åpne XML-fil";
            openFileDialog1.ShowDialog();
            string filenSomSkalApnes = string.Empty;
            filenSomSkalApnes = openFileDialog1.FileName;

            textBox1.Text = openFileDialog1.FileName;

            textBox7.Text = textBox1.Text;
            

            bool fortsett = true;

            StreamReader myFile = new StreamReader(filenSomSkalApnes);
            //textBox1.Text = string.Empty;

            while (fortsett)
            {
                textBox4.Text += myFile.ReadLine();
                textBox4.Text += Environment.NewLine;
                fortsett = !myFile.EndOfStream;
            }

            //Henter opp metoden listboksen
            
            
           

            textBox3.Enabled = true;

            //while (textBox6 != null)

            if (textBox6 != null)
            {
                textBox3.Enabled = false;
            }

           
             
            XmlDocument xmlDoc = new XmlDocument(); //* create an xml document object. 

            xmlDoc.Load("test2.xml"); //* load the XML document from the specified file. 

            XmlNode rootnode = xmlDoc.FirstChild;

            foreach (XmlNode xmlEle in rootnode.ChildNodes)
            {
                string s = xmlEle.Name;
                MessageBox.Show(s);
            }

            

            XmlNode appNode = rootnode.NextSibling;
            textBox3.Text = (appNode.Name);
            string a = (appNode.Name);

            //XmlNode appNode2 = appNode.NextSibling;
            //textBox5.Text = (appNode2.Name);


            textBox2.Text = appNode.Name;
 
            
            listboksen();

            myFile.Close();

        }

        



        private void button2_Click(object sender, EventArgs e)
        {
            XmlTextReader reader = new XmlTextReader("test.xml");

            while (reader.Read())
            {
                Console.WriteLine(reader.Name);
            }
            Console.ReadLine();
        }


        



        //Gjør at når man skriver i den ene tekstboksen, blir den andre grået ut.
        /*
        private void textBox6_TextChanged(object sender, EventArgs e)
        {

            textBox3.Enabled = false;

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

            textBox6.Enabled = false;
          


        }
        */
        

        private void button3_Click_2(object sender, EventArgs e)
        {

            XmlDocument xmlDoc = new XmlDocument(); //* create an xml document object. 

            xmlDoc.Load("test2.xml"); //* load the XML document from the specified file. 

            XmlNode rootnode = xmlDoc.FirstChild;
            

            XmlNode appNode = rootnode.NextSibling;
            textBox3.Text = (appNode.Name);
            string a = (appNode.Name);
            //listBox2.Items.Add = ("test");

            
            XmlNode settingsNode = appNode.FirstChild;
            string lKey = "Length Unit";
            foreach (XmlNode aNode in settingsNode.ChildNodes)
            {

                if (aNode.Attributes[0].Value.ToString().Equals(lKey))
                {
                    string n = aNode.Attributes[1].Value.ToString();
                    textBox6.Text = n;
                    MessageBox.Show(aNode.Attributes[1].Value.ToString());
                    
                }

            }
        }


        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void textBox8_TextChanged(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            XmlDocument xmlDoc = new XmlDocument(); //* create an xml document object. 

            xmlDoc.Load("test2.xml"); //* load the XML document from the specified file. 

            XmlNode rootnode = xmlDoc.FirstChild;

            foreach (XmlNode xmlEle in rootnode.ChildNodes)
            {
                string s = xmlEle.Name;
                textBox9.Text = s;
                MessageBox.Show(s);
            }

            

        }

        /*private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            XmlDocument xmlDoc = new XmlDocument(); //* create an xml document object. 

            xmlDoc.Load("test2.xml"); //* load the XML document from the specified file. 

            XmlNode rootnode = xmlDoc.FirstChild;

            MessageBox.Show(xmlDoc.FirstChild.ToString);
        }*/
    }
}
